package com.game.majiang.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.game.majiang.algorithm.doudizhu.DoudizhuAIRandomcardsServer;
import com.game.majiang.vo.CYVo;
import com.game.majiang.vo.CheckCodeVo;
import com.game.majiang.vo.CrosstalkVo;
import com.game.majiang.vo.GameEndQuotationsVo;
import com.game.majiang.vo.MinggancikuVo;
import com.game.majiang.vo.RankingListStructrue;
import com.game.majiang.vo.RollMessageDataVo;
import com.game.majiang.vo.RoomConfigVo;
import com.game.majiang.vo.everydaysignVo;
import com.game.server.db.SqlHelper;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.util.IdWorker;

import lombok.Getter;
import lombok.Setter;;

/***
 * 数据管理类
 * 
 * @author 
 * 
 */
public class DataManager {

	/**
	 * 系统参数
	 */
	@Setter
	@Getter
	private HashMap<String,String> sys_parameter;
	/**
	 * 系统参数
	 */
	@Setter
	@Getter
	private HashMap<String,Integer> sys_parameterServerType;
	@Setter
	@Getter
	private HashMap<Integer, RoomConfigVo> roommap;
	@Setter
	@Getter
	HashMap<Integer, everydaysignVo> everydaysignmap;

	@Setter
	@Getter
	public DoudizhuAIRandomcardsServer doudizhuaiserver;
	@Setter
	@Getter
	public ArrayList<CYVo> chengyujielongList;

	@Setter
	@Getter
	private HashMap<String,String> chexuanzhongjiangparam;
	@Setter
	@Getter
	private HashMap<String,String> chexuangameneedparam;
	@Setter
	@Getter
	private HashMap<String,String> chexuanjiangchiparam;
	
	@Setter
	@Getter
	private ArrayList<MinggancikuVo> minganciku;
	@Setter
	@Getter
	private int tablenumber;//桌子号
	
	@Setter
	@Getter
	private boolean serverLoginBool = true;//服务器登录状态
	@Setter
	@Getter
	private boolean enterRoom = true;//进房间
	
	
	@Setter
	@Getter
	private ArrayList<CrosstalkVo>  ctv;//过场界面提示语
	
	
	@Setter
	@Getter
	private ArrayList<GameEndQuotationsVo> geq;//游戏结束要出现的语录
	
	@Setter
	@Getter
	HashMap<String, ArrayList<RankingListStructrue>> rankinglist = new HashMap<String, ArrayList<RankingListStructrue>>();
	
	@Setter
	@Getter
	private HashMap<String,CheckCodeVo> verification = new HashMap<String, CheckCodeVo>();//验证码
	
	@Setter
	@Getter
	private ArrayList<RollMessageDataVo>  rollMessageData ;//滚动公告数据
	
	@Setter
	@Getter
	private HashMap<Integer,Integer> usermaimawin = new HashMap<Integer,Integer>();//今天买马的输赢
	
	@Setter
	@Getter
	private HashMap<Integer,Integer> yapqingma=new HashMap<Integer,Integer>();//（邀请码 /俱乐部id）

	//--------------------------------------------------------------------

	@Setter
	@Getter
	private HashMap<Integer, UserInfoData> mapUserData = new HashMap<>();//所有用户基础数据

	@Setter
	@Getter
	private HashMap<Integer, XyClubInfo> mapClub = new HashMap<>();//xy 俱乐部列表

	@Setter
	@Getter
	private HashMap<Integer, XyUnionInfo> mapUnion = new HashMap<>();//xy 联盟列表

	@Getter
	private IdWorker worker = new IdWorker(1,1,1);//雪花ID  唯一ID

	
	public int getgoldbyindex(int index)
	{
		everydaysignVo vo = everydaysignmap.get(index);
		return vo.gold;
	}

	public int checkeverydaysign(int val)
	{
		int index = 1;
		for(int i=1; i<=14; i++)
		{
			everydaysignVo vo = everydaysignmap.get(i);
			if(vo.check(val))
			{
				index = i;
				break;
			}
		}
		return index;
	}
	public Integer[] geteverydaysigndata()
	{
		Integer data[] = new Integer[14];
		for(int i=0; i<14; i++)
		{
			data[i] = everydaysignmap.get(i+1).gold;
		}
		return data;
	}

	//单例
	private static DataManager instance;

	private short tableSequence = 0;
	//    @Setter
	//    @Getter
	//    private HashMap<String,String> radioAndMessage;
	//    
	public static DataManager getInstance() {
		if (instance == null) instance = new DataManager();
		return instance;
	}

	private DataManager()
	{
	}

	public String systemValue(String key) {
		return sys_parameter.get(key);
	}

	public int systemServerType(String key) {
		return sys_parameterServerType.get(key);
	}
	
	//	public String radioAndMessageValue(String key) {
	//		return radioAndMessage.get(key);
	//	}


	public synchronized short  getTableSequence()
	{
		return ++tableSequence;
	}

	public int hUsermaimawin(int uid) {
		Object obj = usermaimawin.get(uid);
		int win = 0;
		if (obj == null) {
			usermaimawin.put(uid, 0);
		} else {
			win = (int) obj;
		}
		return win;
	}


	/**
	 * 查询一个唯一的随机ID
	 * @param tableName  要查询的表名
	 * @return
	 */
	public synchronized int findOnlyID(String tableName,String fieldName){
		LogManager.getInstance().Log("要查询的表名:"+tableName);
		LogManager.getInstance().Log("要查询的字段:"+fieldName);
		int id = 0;
		Map<Integer,Integer> map = new HashMap<>();
		map.put(100000,899999);
		map.put(1000000,8999999);
		map.put(10000000,89999999);
		boolean nextFlg = true;
		for (Map.Entry<Integer,Integer> entry:map.entrySet()) {
			if (!nextFlg){
				break;
			}
			for (int i = 0; i<10;i++){
				String countsql = "SELECT DISTINCT(temp.rn) FROM (SELECT IFNULL(FLOOR("+entry.getKey()+" + RAND() * "+entry.getValue()+"),0) AS rn FROM "+tableName+") AS temp where temp.rn NOT IN (select "+fieldName+" FROM "+tableName+") LIMIT 0,1";
				Object scalar = SqlHelper.execScalar(countsql);
				if (scalar!=null ){
					id = (int) Double.parseDouble(scalar.toString());
					nextFlg = false;
					break;
				}
			}
		}
		if (id==0){
			String sql = "select count(id) from "+tableName;
			Object scalar = SqlHelper.execScalar(sql);
			if ((int) Double.parseDouble(scalar.toString()) ==0 ){
				id = (int)((Math.random()*9+1)*100000);
			}else{
				return findOnlyID(tableName,fieldName);
			}
		}
		return  id;
	}

	public synchronized long getTabelOnly() {
		return worker.nextId();
	}
}
